/**
 * 
 */
package com.ttl.erp.order.yd;

import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.ttl.common.db.ADAO;
import com.ttl.common.util.CommonUtils;

/**
 *  @author :  Surasak Lekmart
 *  Created :  17 Jul 2012 11:51:22 
 *  Project :  ittl
 */
public class TTL_ORDER_YD_DAO extends ADAO {

	/**
	 *  Database :
	 *     Table :
	 *      View :
	 *  Descript.:
	 */
	public TTL_ORDER_YD_DAO() {
		// TODO Auto-generated constructor stub
	}

	public boolean addList(ArrayList<TTL_ORDER_YD> al) {
		Session session = null;
	    
		try {
			sessionFactory = HibernateUtil.getSessionFactory2();
			session = sessionFactory.openSession();
			session.beginTransaction();
			for (TTL_ORDER_YD ord : al) {
				session.save(ord);	
			}
			
			session.getTransaction().commit();
			return true;
		}catch(Exception e) {
			System.out.println(e.toString());
			return false;
		}finally {
			session.close();
		}
	}
	
	public List find(String hql) {
		List result = null;
		Session session = null;

		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory2();
			session = sessionFactory.openSession();
			
			session.beginTransaction();
			
			Query q = session.createQuery(hql);
			result = q.list();
			
			session.getTransaction().commit();						
			
		}catch(Exception e) {			
			System.out.println(e.toString());
		}finally {
			session.close();
		}
		return result;
	}

	public boolean updateList(TTL_ORDER_YD ordNew, int rowOrder) {
		Session session = null;
	    
		try {
			sessionFactory = HibernateUtil.getSessionFactory2();
			session = sessionFactory.openSession();
			session.beginTransaction();
			
			Query q = session.createQuery("FROM TTL_ORDER_YD WHERE rowOrder="+rowOrder);
			List result = q.list();
			for (Object obj : result) {
				TTL_ORDER_YD ord = (TTL_ORDER_YD)obj;
				
				ord.setOrder_no(ordNew.getOrder_no());
				ord.setOrder_date(ordNew.getOrder_date());
				ord.setOrder_plan(ordNew.getOrder_plan());
				ord.setTtl_plan(ordNew.getTtl_plan());
				ord.setCol_no(ordNew.getCol_no());
				ord.setCol_sub(ordNew.getCol_sub());
				ord.setLot_no(ordNew.getLot_no());
				ord.setCount(ordNew.getCount());
				ord.setPbls(ordNew.getPbls());
				ord.setLbs(ordNew.getLbs());
				ord.setShade(ordNew.getShade());
				ord.setW_p(ordNew.getW_p());
				ord.setRec(ordNew.getRec());
				ord.setOrg(ordNew.getOrg());
				ord.setCheese(ordNew.getCheese());
				ord.setFuai(ordNew.getFuai());
				ord.setIsCancel(0);

				
				session.save(ord);	
			}
			
			session.getTransaction().commit();
			return true;
		}catch(Exception e) {
			System.out.println(e.toString());
			return false;
		}finally {
			session.close();
		}
	}

	public boolean delete(int rowOrder) {
		Session session = null;
	    
		try {
			sessionFactory = HibernateUtil.getSessionFactory2();
			session = sessionFactory.openSession();
			session.beginTransaction();
			
			Query q = session.createQuery("FROM TTL_ORDER_YD WHERE rowOrder="+rowOrder);
			List result = q.list();
			for (Object obj : result) {
				TTL_ORDER_YD ord = (TTL_ORDER_YD)obj;

				ord.setIsCancel(1);
				session.save(ord);	
			}
			
			session.getTransaction().commit();
			return true;
		}catch(Exception e) {
			System.out.println(e.toString());
			return false;
		}finally {
			session.close();
		}
	}
}
